package com.wd.live.base

import android.os.Bundle
import android.service.voice.VoiceInteractionSession.ActivityId
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType

/**
 * 作者: 张智聪
 * 时间: 2024/10/15
Time:  16:05
 * 描述:
 */
abstract class BaseFragment <VM:ViewModel,D:ViewDataBinding>(private  val layoutId: Int):Fragment(){
    lateinit var viewmodel:VM
    lateinit var binding:D
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
       binding=DataBindingUtil.inflate(inflater,layoutId,container,false)
        binding.lifecycleOwner=this
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val type=javaClass.genericSuperclass as ParameterizedType
        val classVM=type.actualTypeArguments[0] as Class<VM>
        viewmodel= ViewModelProvider(this)[classVM]
        initViews()
        dataObserver()
    }
    abstract fun dataObserver()
    abstract fun initViews()
    override fun onDestroy() {
        super.onDestroy()
        if(binding!=null){
            binding.unbind()
        }
    }
}